//
//  BootPageVC.m
//  CrossCityCarDriver
//
//  Created by 李亚军 on 17/2/10.
//  Copyright © 2017年 李亚军. All rights reserved.
//

#import "BootPageVC.h"
#import "BootPageImageCell.h"
#import "AppDelegate.h"
#import <QXDataManager/QXDataManager.h>
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height

@interface BootPageVC ()<UICollectionViewDelegate , UICollectionViewDataSource , UICollectionViewDelegateFlowLayout>
/**
 进入app的背景图
 */
@property (weak, nonatomic) IBOutlet UIView *enterBackView;


/**
 进入app的背景图的顶端距离屏幕顶端(电池条下面)的距离
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *enterBackViewMarginToTop;

/**
 引导图数组
 */
@property(nonatomic , strong) NSArray *bootPageImageArray;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property(nonatomic , copy) void(^startAppActionBlock)();

@end

@implementation BootPageVC

static NSString *const hasEeverShowBootPage = @"HasOverShowBootPageVC";

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.bootPageImageArray == nil) {
        
        [self configureBootPageImage];
    }
    
    [self configureEnterBackViewMarginToTop];
    
    self.enterBackView.hidden = YES;
}


/**
 初始化引导页
 */
+ (BootPageVC *)initBootPageVCWithBootPageImageArray:(NSArray *)bootPageImageArray startAppBlock:(void (^)())startAppBlock {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"BootPage" bundle:[NSBundle mainBundle]];
    BootPageVC *bootPageVC = [storyboard instantiateViewControllerWithIdentifier:@"BootPageVCID"];
    bootPageVC.startAppActionBlock = startAppBlock;
    bootPageVC.bootPageImageArray = bootPageImageArray;
    [bootPageVC StartBootpageVCToRootVC];
    
    return bootPageVC;
}


- (void)StartBootpageVCToRootVC {
    
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

//    BOOL isFirstStartApp = [[NSUserDefaults standardUserDefaults] boolForKey:hasEeverShowBootPage];
    BOOL isFirstStartApp = [[QXDataManager getValueForKey:hasEeverShowBootPage] boolValue];
    if (isFirstStartApp == NO) { // 如果没有启动个App,那么设置启动页
        
        appDelegate.window.rootViewController = self;
//        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:hasEeverShowBootPage]; // App曾经启动过这里进行标记
        [QXDataManager saveValue:@"1" forKey:hasEeverShowBootPage];
    
    }
}



/**
 配置默认图片
 */
- (void)configureBootPageImage {
    
    NSMutableArray *arrPage = [NSMutableArray array];

    if (ScreenWidth == 320 && ScreenHeight == 480) {
        [arrPage addObject:@"guide01_4"];
        [arrPage addObject:@"guide02_4"];
        [arrPage addObject:@"guide03_4"];
    }else {
        [arrPage addObject:@"guide01"];
        [arrPage addObject:@"guide02"];
        [arrPage addObject:@"guide03"];
    }
    
    self.bootPageImageArray = arrPage;
}


/**
 根据屏幕比例配置进入按钮距离顶部的距离
 */
- (void)configureEnterBackViewMarginToTop {
    
    if (ScreenWidth == 320 && ScreenHeight == 480) {
        
        self.enterBackViewMarginToTop.constant = 410.00 / 480.00 * ScreenHeight;
        
    }else {
        
        self.enterBackViewMarginToTop.constant = 550.00 / 667.00 * ScreenHeight;
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.bootPageImageArray.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell;
    
    cell = [self dealBootPageImageCellWithCollectionView:collectionView indexPath:indexPath];
    
    return cell;
}

- (BootPageImageCell *)dealBootPageImageCellWithCollectionView:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath {
    
    BootPageImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BootPageImageCellID" forIndexPath:indexPath];
    
    if (self.bootPageImageArray.count > 0) {
        
        cell.imageView.image = [UIImage imageNamed:self.bootPageImageArray[indexPath.row]];
    }

    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
//    if (self.bootPageImageArray.count <= 0) {
//        

//        return;
//    }
//    
//    if (indexPath.row == self.bootPageImageArray.count - 1) {
//        
//        self.enterBackView.hidden = NO;
//    }else {
//        
//        self.enterBackView.hidden = YES;
//    }
    
    self.enterBackView.hidden = YES;
}


#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(ScreenWidth, ScreenHeight);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.bootPageImageArray.count - 1) {
        
        if (self.startAppActionBlock) {
            self.startAppActionBlock();
        }
    }
}

#pragma mark - Action
- (IBAction)enterAppAction:(UIButton *)sender {
    
    if (self.startAppActionBlock) {
        self.startAppActionBlock();
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
